import router from '@/router';
import { routeInfo } from '@/router/info';
import { MenuItem } from '@/router/menu';
// import { SubSystemResponse } from '@/services/auth';
import { defineStore } from 'pinia';
import { getAuthsFromSubSystem, getMenusFromRoutes } from './helper';

interface RouteStoreState {
  /** 权限列表 */
  auths: Set<string>;
}

/**
 * 路由、菜单、权限 store
 */
export const useRouteStore = defineStore({
  id: 'Route',

  state: (): RouteStoreState => ({
    auths: new Set(),
  }),

  getters: {
    /**
     * 菜单
     */
    frontMenus(): MenuItem[] {
      const menus = getMenusFromRoutes(router.getRoutes(), this.auths, 'front');
      // console.log('>>>>>>> get front menus', menus);
      return menus;
    },

    /**
     * 系统设置菜单
     */
    backMenus(): MenuItem[] {
      const menus = getMenusFromRoutes(router.getRoutes(), this.auths, 'back');
      // console.log('>>>>>>> get back menus', menus);
      return menus;
    },
  },

  actions: {
    // 更新权限
    setAuths(
      // frontNavs: SubSystemResponse | undefined,
      // backNavs: SubSystemResponse | undefined,
      isStudent = false,
    ) {
      if (isStudent) {
        routeInfo.accountType = 'student';
      } else {
        routeInfo.accountType = 'teacher';
      }

      const auths = new Set([
        // ...getAuthsFromSubSystem(frontNavs),
        // ...getAuthsFromSubSystem(backNavs),
      ]);

      // console.debug('[auth] set auths', auths);

      this.auths = auths;
      routeInfo.setAuths(this.auths);
    },

    // 重置权限
    reset() {
      this.auths = new Set();
      routeInfo.setAuths(this.auths);
    },
  },
});
